package com.drug.web.controller.wms;

import com.drug.common.annotation.Log;
import com.drug.common.constant.HttpStatus;
import com.drug.common.core.controller.BaseController;
import com.drug.common.core.domain.AjaxResult;
import com.drug.common.core.page.TableDataInfo;
import com.drug.common.enums.BusinessType;
import com.drug.common.utils.DateUtils;
import com.drug.common.utils.poi.ExcelUtil;
import com.drug.wms.domain.*;
import com.drug.wms.page.BusGoodsStockInfoPage;
import com.drug.wms.service.IBusFileService;
import com.drug.wms.service.IBusGoodsBaseInfoService;
import com.drug.wms.service.IBusGoodsStockDetailService;
import com.drug.wms.service.IBusGoodsStockInfoService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商品入库清单Controller（小程序端）
 *
 * @author ruoyi
 * @date 2021-12-01
 */
@RestController
@RequestMapping("/wms/applet/goodsStock")
public class BusGoodsStockInfo2Controller extends BaseController {
    @Autowired
    private IBusGoodsStockInfoService busGoodsStockInfoService;
    @Autowired
    private IBusGoodsStockDetailService busGoodsStockDetailService;
    @Autowired
    private IBusGoodsBaseInfoService busGoodsBaseInfoService;
    @Autowired
    private IBusFileService busFileService;

    public static void main(String[] args) {
        DateUtils.dateTime("yyyy-mm-dd", "fdsafds");
    }

    /**
     * 查询单据
     */
    @GetMapping("/list")
    public TableDataInfo list(BusGoodsStockInfo busGoodsStockInfo, @RequestParam(value = "queryParam", required = false) String queryParam) {
        List<BusGoodsStockInfo> list = null;
        startPage();
        if (!StringUtils.isEmpty(queryParam)) {
            Date date = null;
            try {
                date = DateUtils.dateTime("yyyy-mm-dd", queryParam);
                busGoodsStockInfo.setEntryDate(date);
                busGoodsStockInfo.setHandleUserId(Long.valueOf(queryParam));
            } catch (Exception e) {
                busGoodsStockInfo.setReceivingWarehouse(queryParam);
                busGoodsStockInfo.setSupplyCompany(queryParam);
                try{
                    busGoodsStockInfo.setHandleUserId(Long.valueOf(queryParam));
                }catch (Exception e1){

                }
            }
            list = busGoodsStockInfoService.selectBusGoodsStockInfoListByOr(busGoodsStockInfo);
        } else {
            list = busGoodsStockInfoService.selectBusGoodsStockInfoList(busGoodsStockInfo);
        }
        ArrayList<BusGoodsStockInfoRight> result = new ArrayList<>();
        //手动处理,为前端增加right：0
        for (BusGoodsStockInfo b : list) {
            BusGoodsStockInfoRight jsonObject = new BusGoodsStockInfoRight();
            if (list.size() == 1) {
                //获取图片信息
                List<BusFile> busFiles = busFileService.listByBusId(b.getId());
                jsonObject.setFiles(busFiles);
            }
            BeanUtils.copyProperties(b, jsonObject);
            result.add(jsonObject);
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(result);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }


    /**
     * 导出商品入库清单列表
     */
    @Log(title = "商品入库清单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BusGoodsStockInfo busGoodsStockInfo) {
        List<BusGoodsStockInfo> list = busGoodsStockInfoService.selectBusGoodsStockInfoList(busGoodsStockInfo);
        ExcelUtil<BusGoodsStockInfo> util = new ExcelUtil<BusGoodsStockInfo>(BusGoodsStockInfo.class);
        return util.exportExcel(list, "商品入库清单数据");
    }


    /**
     * 查询商品列表
     *
     * @param stockId 单据id
     * @param id      药品id
     * @param barcode 条形码
     * @return
     */
    @GetMapping(value = "/list/{stockId}")
    public TableDataInfo getDetailInfo(@PathVariable("stockId") String stockId, @RequestParam(required = false) String id, @RequestParam(required = false) String barcode,@RequestParam(required = false) String queryParam) {
        startPage();
        BusGoodsStockDetail busGoodsStockDetail = new BusGoodsStockDetail();
        busGoodsStockDetail.setStockId(stockId);
        List<BusGoodsStockDetail> result = new ArrayList<>();
        if (!StringUtils.isEmpty(id)) {
            busGoodsStockDetail.setId(id);
            result = busGoodsStockDetailService.selectBusGoodsStockDetailList(busGoodsStockDetail);
        } else if (barcode != null) {
            BusGoodsBaseInfo busGoodsBaseInfo = new BusGoodsBaseInfo();
            busGoodsBaseInfo.setBarcode(barcode);
            List<BusGoodsBaseInfo> busGoodsBaseInfos = busGoodsBaseInfoService.selectBusGoodsBaseInfoList(busGoodsBaseInfo);
            TableDataInfo tableDataInfo = CheckData(barcode, stockId, result);
            if (tableDataInfo != null) {
                return tableDataInfo;
            }
            BusGoodsStockDetail details = new BusGoodsStockDetail();
            details.setGoodsNo(busGoodsBaseInfos.get(0).getGoodsNo());
            details.setGoodsName(busGoodsBaseInfos.get(0).getGoodsName());
            details.setBarcode(busGoodsBaseInfos.get(0).getBarcode());
            details.setNorms(busGoodsBaseInfos.get(0).getNorms());
            details.setBasicUnitNo(busGoodsBaseInfos.get(0).getBasicUnitNo());
            details.setStockId(stockId);
            details.setApprovalNumber(busGoodsBaseInfos.get(0).getApprovalNumber());
            details.setManufacturerNo(busGoodsBaseInfos.get(0).getManufacturerNo());
            //获取单据信息
            BusGoodsStockInfo busGoodsStockInfo = busGoodsStockInfoService.selectBusGoodsStockInfoById(stockId);
            details.setReceivingNo(busGoodsStockInfo.getReceivingWarehouse());
            result.add(details);
        } else {
            //查询条件
            if(!StringUtils.isEmpty(queryParam)){
                busGoodsStockDetail.setGoodsName(queryParam);
            }
            result = busGoodsStockDetailService.selectBusGoodsStockDetailListByOr(busGoodsStockDetail);
            ArrayList<BusGoodsStockDetailRight> rights = new ArrayList<>();
            //手动处理,为前端增加right：0
            for (BusGoodsStockDetail b : result) {
                BusGoodsStockDetailRight jsonObject = new BusGoodsStockDetailRight();
                BeanUtils.copyProperties(b, jsonObject);
                rights.add(jsonObject);
            }
            return getDataTable(rights);
        }

        return getDataTable(result);
    }

    /**
     * 校验数据
     *
     * @param barcode 条形码
     * @param stockId 单据id
     * @param result  结果集
     * @return
     */
    private TableDataInfo CheckData(String barcode, String stockId, List<BusGoodsStockDetail> result) {
        BusGoodsBaseInfo busGoodsBaseInfo = new BusGoodsBaseInfo();
        busGoodsBaseInfo.setBarcode(barcode);
        List<BusGoodsBaseInfo> busGoodsBaseInfos = busGoodsBaseInfoService.selectBusGoodsBaseInfoList(busGoodsBaseInfo);
        if (CollectionUtils.isEmpty(busGoodsBaseInfos)) {//为查询到商品
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.SUCCESS);
            rspData.setMsg("当前条形码未查询到商品");
            rspData.setRows(result);
            rspData.setTotal(0);
            return rspData;
        }
        return null;
    }


    /**
     * 入库单据(保存)
     */
    @Log(title = "入库单据(保存)", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody BusGoodsStockInfoPage busGoodsStockInfoPage) {
        String s = busGoodsStockInfoService.insertBusGoodsStockInfo(busGoodsStockInfoPage);
        return AjaxResult.success(s);
    }

    /**
     * 新增商品
     */
    @Log(title = "新增商品入库清单详情", businessType = BusinessType.INSERT)
    @PostMapping("addDetail")
    public AjaxResult addDetail(@RequestBody BusGoodsStockDetail[] busGoodsStockDetail) {
        return toAjax(busGoodsStockDetailService.insertBusGoodsStockDetail(busGoodsStockDetail));
    }


    /**
     * 入库单据(提交)
     */
    @Log(title = "入库单据(提交)", businessType = BusinessType.UPDATE)
    @GetMapping("submit")
    public AjaxResult submit(String stockId) {
        BusGoodsStockInfo stockInfo = new BusGoodsStockInfo();
        stockInfo.setId(stockId);
        stockInfo.setInputStatus(1L);
        return toAjax(busGoodsStockInfoService.submitBusGoodsStockInfo(stockInfo));
    }

    /**
     * 修改入库单据
     */
    @Log(title = "商品入库清单", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody BusGoodsStockInfoPage busGoodsStockInfoPage) {
        return AjaxResult.success(busGoodsStockInfoService.updateBusGoodsStockInfo(busGoodsStockInfoPage));
    }

    /**
     * 修改商品
     */
    @Log(title = "修改商品入库清单详情", businessType = BusinessType.UPDATE)
    @PostMapping("editDetail")
    public AjaxResult editDetail(@RequestBody BusGoodsStockDetail[] busGoodsStockDetail) {
        return toAjax(busGoodsStockDetailService.updateBusGoodsStockDetail(busGoodsStockDetail));
    }

    /**
     * 获取最近商品入库清单详细信息
     */
    @GetMapping(value = "/latest/{goodsNo}")
    public AjaxResult getGoodsLatest(@PathVariable("goodsNo") String goodsNo) {
        return AjaxResult.success(busGoodsStockDetailService.selectBusGoodsStockDetailLatestByGoodsNo(goodsNo));
    }

    /**
     * 删除商品
     */
    @Log(title = "删除商品入库清单详情", businessType = BusinessType.DELETE)
    @GetMapping("/removeDetail/{id}")
    public AjaxResult removeDetail(@PathVariable String id) {
        return toAjax(busGoodsStockDetailService.deleteBusGoodsStockDetailById(id));
    }

    /**
     * 删除入库单据
     */
    @Log(title = "商品入库清单", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{stockId}")
    public AjaxResult remove(@PathVariable String stockId) {
        return toAjax(busGoodsStockInfoService.deleteBusGoodsStockInfoById(stockId));
    }
}
